﻿using BCL.WPFComponent.Base;
using Devices;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DevicesWPF
{
    /// <summary>
    /// UCResultConfig.xaml 的交互逻辑
    /// </summary>
    public partial class UCResultConfig : BaseFunctionControl
    {

        private ObservableCollection<ViewResultItemConfig> viweList;
        public UCResultConfig()
        {
            InitializeComponent();
            viweList = new ObservableCollection<ViewResultItemConfig>();
            viweList.Add(new ViewResultItemConfig { Code = "New", EnglishName = "New", Max = "0", Min = "0", Name = "新建项" });
            this.dataGridViewItems.ItemsSource = viweList;
        }

        public UCResultConfig(ResultConfig config)
        {
            InitializeComponent();
            dataGridViewItems.AutoGenerateColumns = false;
            textBoxName.Text = config.Name;
            textBoxCode.Text = config.Code;
            textBoxFormula.Text = config.Formula;
            viweList = new ObservableCollection<ViewResultItemConfig>();
            config.Items?.ForEach(o => viweList.Add(ViewResultItemConfig.Convert(o)));
            this.dataGridViewItems.ItemsSource = viweList;
        }
        public override void LanguageBinding()
        {

        }
        public override void ReloadData(object data)
        {
            base.ReloadData(data);
        }



        public ResultConfig GetResultConfig()
        {
            ResultConfig rc = new ResultConfig();
            rc.Name = textBoxName.Text.Trim();
            rc.Formula = textBoxFormula.Text.Trim();
            if (string.IsNullOrEmpty(rc.Name))
                return null;
            if (!string.IsNullOrEmpty(rc.Formula) && ResultConfig.GetFormula(rc.Formula) == null)
                return null;
            rc.Code = textBoxCode.Text;
            rc.Items = new List<ResultItemConfig>();
            if (viweList != null)
            {
                foreach (var item in viweList)
                {
                    if (!string.IsNullOrEmpty(item.Code))
                        rc.Items.Add(item.ConvertTo());
                }
            }

            return rc;
        }
        private event Action<String> resultNameChanged;
        public event Action<String> ResultNameChanged
        {
            add
            {
                resultNameChanged += value;
            }
            remove
            {
                resultNameChanged -= value;
            }
        }

        private void textBoxName_TextChanged(object sender, TextChangedEventArgs e)
        {
            resultNameChanged?.Invoke(textBoxName.Text);
        }

        private void 添加项ToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            viweList.Add(new ViewResultItemConfig { Code = "New", EnglishName = "New",  Max = "0", Min = "0", Name = "新建项" });
        }

        private void 删除ToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (dataGridViewItems.SelectedItems != null)
                viweList.Remove(dataGridViewItems.SelectedItems as ViewResultItemConfig);
        }

        private void 上移ToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (dataGridViewItems.SelectedItem != null)
            {
                ViewResultItemConfig item = (ViewResultItemConfig)dataGridViewItems.SelectedItems[0];
                int index = viweList.IndexOf(item);
                viweList.Remove(item);
                viweList.Insert(index - 1, item);
                dataGridViewItems.SelectedItem = item;
            }
        }

        private void 下移ToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (dataGridViewItems.SelectedItem != null)
            {
                ViewResultItemConfig item = (ViewResultItemConfig)dataGridViewItems.SelectedItems[0];
                int index = viweList.IndexOf(item);
                viweList.Remove(item);
                viweList.Insert(index + 1, item);
                dataGridViewItems.SelectedItem = item;
            }
        }

        private void 置顶ToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (dataGridViewItems.SelectedItem != null)
            {
                ViewResultItemConfig item = (ViewResultItemConfig)dataGridViewItems.SelectedItems[0];
                viweList.Remove(item);
                viweList.Insert(0, item);
                dataGridViewItems.SelectedItem = item;
            }
        }

        private void 置底ToolStripMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (dataGridViewItems.SelectedItem != null)
            {
                ViewResultItemConfig item = (ViewResultItemConfig)dataGridViewItems.SelectedItems[0];
                viweList.Remove(item);
                viweList.Add(item);
                dataGridViewItems.SelectedItem = item;
            }
        }





        public class ViewResultItemConfig : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private string code;
            private string name;
            private string englishName;
            private string min;
            private string max;
            private string unit;
            private string format;
            private int id;
            private string remarks;

            public string Remarks
            {
                get
                {
                      return remarks;
                }
                set
                {
                    remarks = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Remarks"));
                }
            }

            public  string Display
            {
                get
                {
                    if (!string.IsNullOrEmpty(Format))
                        return Format.Replace("%C", Name).Replace("%A", Code).Replace("%U", Unit).Replace("%E", EnglishName);
                    return Format;
                }
                set {
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Display"));
                }
            }

            //public  int Id
            //{
            //    get
            //    {
            //        return id;
            //    }
            //    set
            //    {
            //        Id = value;
            //        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Id"));
            //        Display = "";
            //    }
            //}
            public  string Code
            {
                get
                {
                    return code;
                }
                set
                {
                    code = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Code"));
                    Display = "";
                }
            }
            public  string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
                    Display = "";
                }
            }
            public  string EnglishName
            {
                get
                {
                    return englishName;
                }
                set
                {
                    englishName = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("value"));
                    Display = "";
                }
            }
            public  string Min
            {
                get
                {
                    return min;
                }
                set
                {
                    min = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Min"));
                    Display = "";
                }
            }
            public  string Max
            {

                get
                {
                    return max;
                }
                set
                {
                    max = value;

                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Max"));
                    Display = "";

                }
            }
            public  string Unit
            {
                get
                {
                    return unit;
                }
                set
                {
                    unit = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Unit"));
                    Display = "";
                }
            }
            public  string Format
            {
                get
                {
                    return format;
                }
                set
                {
                    format = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Format"));
                    Display = "";
                }
            }
            public static ViewResultItemConfig Convert(ResultItemConfig item)
            {
                ViewResultItemConfig view = new ViewResultItemConfig();
                view.id = item.Id;
                view.code = item.Code;
                view.name = item.Name;
                view.englishName = item.EnglishName;
                view.min = item.Min;
                view.max = item.Max;
                view.unit = item.Unit;
                view.format = item.Format;
                view.remarks = item.Remarks;
                return view;
            }

            public ResultItemConfig ConvertTo()
            {
                ResultItemConfig config = new ResultItemConfig();
                config.Id = this.id;
                config.Code = this.code;
                config.Name = this.name;
                config.EnglishName = this.englishName;
                config.Min = this.min;
                config.Max = this.max;
                config.Unit = this.unit;
                config.Format = this.format;
                config.Remarks = this.remarks;
                return config;
            }
        }
    }
}
